-----------------------------------------------------------------------------------------
-- filename : button.lua
-- description : script that ease to make a button and button animation
-----------------------------------------------------------------------------------------

-- Load needed libaries/scripte
local fontfactory = require "fontfactory"

-- local variable
local button = {}

-- Create a simple button with no animation
function button.createButton(scene,name,x,y,func)
	local group = scene.view
	local bg = display.newImageRect( group,"assets/pause/base_btn.png", 133, 30 )
	bg:setReferencePoint(display.TopLeftReferencePoint)
	
	bg.x = x
	bg.y = y

	local btn_text = display.newText(group,name,x,y,fontfactory.dimitry,15)
	btn_text:setReferencePoint(display.CenterReferencePoint)
	
	btn_text.x = x + bg.width/2;
	btn_text.y = y + bg.height/2;
	
	if(func ~= nil) then
		bg:addEventListener("touch", func)
	end
end

-- Create a simple button with scale animation
function button.createButtonScaleAnim(scene,name,x,y,func,scale)
	local group = scene.view
	local bg = display.newImageRect( group,"assets/pause/base_btn.png", 133, 30 )
	bg:setReferencePoint(display.TopLeftReferencePoint)
	
	bg.x = x
	bg.y = y

	local btn_text = display.newText(group,name,x,y,fontfactory.dimitry,15)
	btn_text:setReferencePoint(display.CenterReferencePoint)
	
	btn_text.x = x + bg.width/2;
	btn_text.y = y + bg.height/2;
	
	bg:setReferencePoint( display.CenterReferencePoint )

	local touch = function(event)
		if ( event.phase == "began" ) then
        	bg:scale(scale,scale)
        	btn_text:scale(scale,scale)
		elseif ( event.phase == "ended" or event.phase == "cancelled" ) then
			bg:scale(1,1)
        	btn_text:scale(1,1)
		    if(func ~= nil) then
				func(event)
			end
		end
		return true
	end

	bg:addEventListener("touch", touch)
end

-- Create a simple button with alpha animation
function button.createButtonAlphaAnim(scene,name,x,y,func,alpha)
	local group = scene.view
	local bg = display.newImageRect( group,"assets/pause/base_btn.png", 133, 30 )
	bg:setReferencePoint(display.TopLeftReferencePoint)
	
	bg.x = x
	bg.y = y

	local btn_text = display.newText(group,name,x,y,fontfactory.dimitry,15)
	btn_text:setReferencePoint(display.CenterReferencePoint)
	
	btn_text.x = x + bg.width/2;
	btn_text.y = y + bg.height/2;
	
	bg:setReferencePoint( display.CenterReferencePoint )

	local touch = function(event)
		if ( event.phase == "began" ) then
        	bg.alpha = alpha
        	btn_text.alpha = alpha
		elseif ( event.phase == "ended" or event.phase == "cancelled" ) then
			bg.alpha = 1
        	btn_text.alpha = 1
		    if(func ~= nil) then
				func(event)
			end
		end
		return true
	end

	bg:addEventListener("touch", touch)
end


return button